package com.kaiwukj.android.ufamily.mvp.ui.page.dynamic.fans;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.LogUtils;
import com.kaiwukj.android.mcas.mvp.BasePresenter;
import com.kaiwukj.android.mcas.utils.RxLifecycleUtils;
import com.kaiwukj.android.ufamily.app.MyApplication;
import com.kaiwukj.android.ufamily.mvp.http.callback.ApiCallBack;
import com.kaiwukj.android.ufamily.mvp.http.entity.base.ListResp;
import com.kaiwukj.android.ufamily.mvp.http.entity.event.DynamicAttentionEvent;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.FansResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.UserResult;
import com.kaiwukj.android.ufamily.mvp.http.utils.RxUtils;
import com.kaiwukj.android.ufamily.mvp.ui.page.dynamic.DynamicContract;

import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;

public class FansPresenter extends BasePresenter<DynamicContract.Model, DynamicContract.FansView> {

    public static final int ATTENTION = 101;
    public static final int ATTENTION_CANCEL = 102;

    @Inject
    RxErrorHandler mErrorHandler;

    @Inject
    public FansPresenter(DynamicContract.Model model, DynamicContract.FansView view) {
        super(model, view);
    }

    public void fetchFans(int userId, int startId, int pageSize) {
        mModel.fetchFansResult(userId, startId, pageSize)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<ListResp<FansResult>>(mErrorHandler, mRootView) {
                    @Override
                    public void onSuccess(@NonNull ListResp<FansResult> result) {
                        mRootView.onFetchFans(result.getList(), result.getTotalCount());
                    }
                });
    }

    public void fetchAttention(int userId, int startId, int pageSize) {
        mModel.fetchAttentionResult(userId, startId, pageSize)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<ListResp<FansResult>>(mErrorHandler, mRootView) {
                    @Override
                    public void onSuccess(@NonNull ListResp<FansResult> result) {
                        mRootView.onFetchFans(result.getList(), result.getTotalCount());
                    }
                });
    }

    public void attention(int userId) {
        mModel.attention(userId)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<Integer>(mErrorHandler, mRootView, ATTENTION) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {
                        UserResult userInfo = MyApplication.getInstance().getUserInfo();
                        userInfo.setConcernNum(userInfo.getConcernNum() + 1);
                        MyApplication.getInstance().setUserInfo(userInfo);
                        EventBus.getDefault().post(new DynamicAttentionEvent());
                        LogUtils.d("关注成功");
                    }
                });
    }

    public void cancelAttention(int userId) {
        mModel.cancelAttention(userId)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<Integer>(mErrorHandler, mRootView, ATTENTION_CANCEL) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {
                        UserResult userInfo = MyApplication.getInstance().getUserInfo();
                        if (userInfo.getConcernNum() > 0) {
                            userInfo.setConcernNum(userInfo.getConcernNum() - 1);
                            MyApplication.getInstance().setUserInfo(userInfo);
                            EventBus.getDefault().post(new DynamicAttentionEvent());
                        }
                        LogUtils.d("取消关注");
                    }
                });
    }

}
